package org.west.sky.frame.activiti.study.chapter17;

import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.util.CollectionUtils;
import org.west.sky.frame.activiti.study.util.ActivitiEngineUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * author: chz
 * date: 2025/6/6
 * description: 事务子流程
 */
public class RunDemo17_1_3 extends ActivitiEngineUtil {

    public static void main(String[] args) {
        RunDemo17_1_3 runDemo16_1_2 = new RunDemo17_1_3();
        runDemo16_1_2.run();
    }

    public void run() {
        loadActivitiConfigAndInitEngine("activiti.job.xml");

        ProcessDefinition processDefinition = deployByClassPathResource("processes/chapter17/Chapter17_1_3.bpmn20.xml");

        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processDefinition.getId());

        Task task1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task1.getName() + "：任务节点");
        Random random = new Random();
        Map<String, Object> variables = new HashMap<>();
        int itemNum = random.nextInt(10) + 5;
        System.out.println("====买了" + itemNum + "件商品");
        variables.put("itemNum", itemNum);
        taskService.complete(task1.getId(), variables);


        while (true) {
            List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstance.getId()).list();
            if (CollectionUtils.isEmpty(tasks)) {
                break;
            }
            for (Task task : tasks) {
                System.out.println("====当前流程：" + processInstance.getId() + "进行到：" + task.getName() + "：任务节点");
//                if ("用户取消下单".equals(task.getName())) {
//                    if (random.nextInt(10) > 5) {
//                        System.out.println("====买错了，取消下单");
//                        taskService.complete(task.getId());
//                    }
//                }
                if ("用户支付订单".equals(task.getName())) {
                    if (random.nextInt(10) > 5) {
                        Task task_ = taskService.createTaskQuery().taskId(task.getId()).singleResult();
                        if (null != task_) {
                            System.out.println("====付完钱了");
                            taskService.complete(task.getId());
                        } else {
                            System.out.println("====支付超时了");
                        }
                    } else {
                        System.out.println("====再纠结10秒钟");
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        }


        closeEngine();
    }
}
